package com.hd.manager.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.hd.hdxgddcommon.entity.dto.ResultDto;
import io.jsonwebtoken.impl.crypto.MacProvider;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: LX
 * @Date: 2022/1/4 13:26
 * @Description:
 */
public class TextMessageUtil {

    private static String regionId = "cn-hangzhou";

    private static String accessKeyId = "LTAI5tFUJ6V8cu5jTENZmxvk";

    private static String secret = "BKr3ldY95jODsoCnQGmLQd44wXEHLd";

    private static String SignName = "杭州电子科技大学信工学院";

    private static String TemplateCode = "SMS_231449869";

    public static  Map<String, String> sendMessage(String phone, String code){

        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest re = new CommonRequest();
        re.setMethod(MethodType.POST);
        re.setDomain("dysmsapi.aliyuncs.com");
        re.setVersion("2017-05-25");
        re.setAction("SendSms");
        re.putQueryParameter("SignName", SignName);
        re.putQueryParameter("TemplateCode", TemplateCode);
        re.putQueryParameter("PhoneNumbers", phone);
        String v = "{\"code\":\"" + code + "\"}";
        re.putQueryParameter("TemplateParam", v);
        try {
            CommonResponse response = client.getCommonResponse(re);
            System.out.println(response.getData());
            HashMap<String, String> map = new HashMap<>();
            map.put("success", response.getData());
            return map;
        } catch (ServerException e) {
            e.printStackTrace();
            HashMap<String, String> map = new HashMap<>();
            map.put("fail", "发送失败");
            return map;
        } catch (ClientException e) {
            e.printStackTrace();
            HashMap<String, String> map = new HashMap<>();
            map.put("fail", "发送失败");
            return map;
        }
    }
}
